package com.gzw.scm.wuyong;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import com.google.gson.reflect.TypeToken;
import com.gzw.scm.R;
import com.gzw.scm.adapter.AddressAdapter;
import com.gzw.scm.base.BaseActivity;
import com.gzw.scm.bean.AddressInfo;
import com.gzw.scm.utils.HttpUtils;
import com.gzw.scm.utils.JsonUtil;
import com.gzw.scm.utils.T;
import com.gzw.scm.utils.UrlUtils;
import java.util.ArrayList;
import java.util.List;

import static android.media.CamcorderProfile.get;

/**
 * Created by Administrator on 2017/7/31.
 */

public class SelectAddressActivity extends BaseActivity{
    private Context mContext;
    private ListView listView;
    private Toolbar toolbar;
    private Intent intent;
    private SearchView searchView,searchView_shi;
    private String gysIdStr;
    private Intent mIntent;
    private List<AddressInfo> ls_info,ls_allInfo;
    private AddressAdapter addressAdapter;
    private String type;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_selectaddress);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        searchView = (SearchView) findViewById(R.id.searchView);
        searchView_shi = (SearchView) findViewById(R.id.searchView_shi);
        listView = (ListView) findViewById(R.id.listView);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("选择地址");
        setSupportActionBar(toolbar);
        mIntent=getIntent();
        gysIdStr = mIntent.getStringExtra("gsyIdStr");
        ls_info = new ArrayList<>();
        ls_allInfo = new ArrayList<>();
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 设置搜索文本监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)) {
                    type="xian";
                    GetAddTask getAddTask = new GetAddTask();
                    getAddTask.execute(newText,"xian");

                } else {
                    listView.clearTextFilter();
                }
                return false;
            }
        });

        // 设置搜索文本监听
        searchView_shi.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)) {
                    type="shi";
                    GetAddTask getAddTask = new GetAddTask();
                    getAddTask.execute(newText,"shi");

                } else {
                    listView.clearTextFilter();
                }
                return false;
            }
        });

    }

    /**
     * 获取地址数据请求
     * RDC
     */
    public class GetAddTask extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {

            String url = "name=" + params[0] + "&type=" + params[1];
            return HttpUtils.doPost(UrlUtils.getInstance().getNewJavaUrl("rdcpda.do", "getAdd"), url);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (result == null || result.length() == 0) {
                T.showShort(mContext, "连接服务器异常");
                return;
            }

            ls_info = JsonUtil.getPersons(result, new TypeToken<List<AddressInfo>>() {
            }.getType());
            ls_allInfo.clear();
            ls_allInfo.addAll(ls_info);
            if (ls_allInfo.size() == 0) {
                T.showShort(mContext, "没有查出相应信息!");
            }
            if (listView.getAdapter() == null) {
                addressAdapter = new AddressAdapter(ls_allInfo, mContext,type);
                listView.setAdapter(addressAdapter);
            } else {
                addressAdapter.notifyDataSetChanged();
            }
            clickListView();
        }
    }


    /**
     * Created by Wangzhiqiang on 2015-10-14
     * Listview点击事
     */
    private void clickListView() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AddressInfo addressInfo = ls_allInfo.get(position);
                String addString = null;
                if(type.equals("shi")){
                    addString = addressInfo.getShengid()+","+addressInfo.getShengname()+","+
                            addressInfo.getShiid()+","+addressInfo.getShiname()+",,";
                }else{
                    addString = addressInfo.getShengid()+","+addressInfo.getShengname()+","+
                            addressInfo.getShiid()+","+addressInfo.getShiname()+","+
                    addressInfo.getXianid()+","+addressInfo.getXianname();
                }
                Intent mIntent = new Intent();
                mIntent.setClass(mContext, CKActivity.class);
                mIntent.putExtra("gsyIdStr",gysIdStr);
                mIntent.putExtra("addString",addString);
                startActivityForResult(mIntent, 1);// 请求码
            }
        });

    }
}
